package com.tongshu.miaosha.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {
    public static final String MIAOSHA_QUEUE = "miaosha.queue";
    public static final String QUEUE = "queue";
    public static final String TOPIC_QUEUE1 = "topic.queue1";
    public static final String HEADER_QUEUE = "header.queue";
    public static final String TOPIC_QUEUE2 = "topic.queue2";
    public static final String TOPIC_EXCHANGE = "topicExchange";
    public static final String FANOUT_EXCHANGE = "fanoutExchange";
    public static final String HEADERS_EXCHANGE = "headersExchange";

    /**
     * Direct模式，定向 交换机Exchange
     * @return
     */
    @Bean
    public Queue queue() {
        // 队列名称 是否持久化
        return new Queue(QUEUE, true);
    }

    @Bean
    public Queue miaoshaQueue() {
        return new Queue(MIAOSHA_QUEUE, true);
    }

    /**
     * Topic模式，通配符 交换机Exchange
     */
    @Bean
    public Queue topicQueue1() {
        return new Queue(TOPIC_QUEUE1,true);
    }
    @Bean
    public Queue topicQueue2() {
        return new Queue(TOPIC_QUEUE2,true);
    }
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }
    @Bean
    public Binding topicBinding1(@Qualifier("topicQueue1")Queue queue,
                                 @Qualifier("topicExchange")TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("topic.key1");
    }
    @Bean
    public Binding topicBinding2(@Qualifier("topicQueue2")Queue queue,
                                 @Qualifier("topicExchange")TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("topic.#");
    }

    /**
     * Fanout模式，广播模式 交换机Exchange
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }
    @Bean
    public Binding FanoutBinding1(@Qualifier("fanoutExchange")FanoutExchange exchange,
                                  @Qualifier("topicQueue1")Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean
    public Binding FanoutBinding2(@Qualifier("fanoutExchange")FanoutExchange exchange,
                                  @Qualifier("topicQueue2")Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    /**
     * Header模式 交换机Exchange
     * @return
     */
    @Bean
    public HeadersExchange headersExchange() {
        return new HeadersExchange(HEADERS_EXCHANGE);
    }
    @Bean
    public Queue headerQueue() {
        return new Queue(HEADER_QUEUE,true);
    }
    @Bean
    public Binding HeadersBinding(@Qualifier("headerQueue")Queue queue,
                                  @Qualifier("headersExchange")HeadersExchange exchange) {
        Map<String, Object> map = new HashMap<>();
        map.put("header1", "value1");
        map.put("header2", "value2");
        // 消息只有完全匹配map中的所有对应值方可发送到正确的交换机
        return BindingBuilder.bind(queue).to(exchange).whereAll(map).match();
    }
}
